<?php

class CompanySource extends Ad {

    public $advertisement = '';
    public $businessType = '';
    public $typeOfService = array();
    public $eblast = array();

    /**
     * combine rules
     * @return array
     */
    public function rules() {
	return CMap::mergeArray(parent::rules(), array(
		    array('contact_name, contact_email', 'required'),
		    array('advertisement, businessType, typeOfService, eblast', 'safe')
	));
    }

    public function relations() {
	return CMAP::mergeArray(parent::relations(), array(
		    'eBlast' => array(self::HAS_ONE, 'Eblast', 'ad_id')
	));
    }

    function defaultScope() {
	return array(
	    'condition' => "`type`= " . self::TYPE_COMPANY_SOURCE,
	);
    }

    /**
     * rewrite type
     */
    public function beforeValidate() {
	parent::beforeValidate();

	$this->type = self::TYPE_COMPANY_SOURCE;

//	if ($this->expireDate != '') {
//	    $this->expire_time = strtotime($this->expireDate);
//	}
//	if ($this->title == '') {
//	    $this->title = 'Company source';
//	}
	return true;
    }

    /**
     * before save action
     */
    public function afterSave() {
	parent::afterSave();

	if ($this->isNewRecord) {
	    $meta = new AdMeta();

	    //add product type to meta
	    $meta->key = 'typeOfService';
	    $meta->value = CJSON::encode($this->typeOfService);
	    $id = $this->addMeta($meta);
	    if ($this->eblast) {
		$eblast = new Eblast();
		$eblast->attributes = $this->eblast;
		$eblast->ad_id = $this->ad_id;
		$eblast->status = Eblast::STATUS_PENDING;
		$eblast->start_date = date('Y-m-d', strtotime($this->eblast['start_date']));
		$eblast->save();
	    }
	} elseif ($this->scenario == self::SCENARIO_UPDATE) {
	    //update ad meta
	    $typeOfServiceMeta = AdMeta::model()->findByAttributes(array(
		'ad_id' => $this->ad_id,
		'key' => 'typeOfService'
	    ));
	    if (!$typeOfServiceMeta) {
		$typeOfServiceMeta = new AdMeta();
		$typeOfServiceMeta->key = 'typeOfService';
	    }
	    $typeOfServiceMeta->value = CJSON::encode($this->typeOfService);
	    $typeOfServiceMeta->save();

	    unset($typeOfServiceMeta);
	}
    }

    public static function getBusinessTypes() {
	return array(
	    'manufacturer' => 'Manufacturer',
	    'distributor' => 'Distributor',
	    'techical_service' => 'Technical Services',
	    'other' => 'Other'
	);
    }

    public static function getServiceTypes() {
	return array(
	    'press' => 'Press',
	    'pre-press' => 'Pre-press',
	    'finishing' => 'Finishing',
	    'parts' => 'Parts',
	    'supplies' => 'Supplies'
	);
    }

    public function getServiceType() {
	switch ($this->productType) {
	    case 'press' : return 'Press';
	    case 'pre-press' : return 'Pre press';
	    case 'finishing' : return 'Finishing';
	    case 'parts' : return 'Parts';
	    case 'supplies' : return 'Supplies';
	}
    }

    public function afterFind() {
	//bind all
	parent::afterFind();

	//render meta
	$metaData = app()->cache->get('ad_meta_data_' . $this->ad_id);
	if (!$metaData) {
	    $metaData = AdMeta::model()->findAll('ad_id = ' . $this->ad_id);
	}

	//bind value
	foreach ($metaData as $meta) {
	    if ($meta->key == 'typeOfService') {
		$this->typeOfService = json_decode($meta->value);
	    }
	}
    }

    /*
     * get number of feature
     */

    public static function getAllFeature() {
	return db()->createCommand('SELECT COUNT(`ad_id`) as `num_featured`, MIN(expire_featured) as min_expire FROM {{ads}} WHERE expire_featured > ' . time())->queryRow();
    }

    /**
     * get min expire time of feature
     */
    public static function getMinExpireFeature() {
	//return db()->createCommand('SELECT MIN(expire_featured) as FROM {{ads}} WHERE expire_featured > ' . time())->queryAll();
    }

    /*
     * 
     */

    public function afterDelete() {
	parent::afterDelete();
	//delete old image
	if ($this->image) {
	    deleteFile(UPLOAD_PATH . $this->image);
	}
	//delete featured
	if ($this->advertisement) {
	    deleteFile(UPLOAD_PATH . 'feature/' . $this->advertisement);
	}
	//delete eblast
	$eblast = Eblast::model()->find('ad_id=' . $this->ad_id);
	$eblast->delete();
    }

}

